package com.laboratory.stream;

import java.util.*;
import java.util.stream.Collectors;

public class ConvertTest {

    public static void main(String[] args) {
//        listMapToList();
        listGroup();
    }

    /**
     * 扩展：取对象某字段，合并转为逗号分隔字符串
     */
    public static void listMapToList() {
        List<Map> list = new ArrayList<>();
        Map<Object, Object> map1 = new HashMap<>();
        map1.put("code", "1");
        map1.put("name", "一");
        list.add(map1);
        Map<Object, Object> map2 = new HashMap<>();
        map2.put("code", "2");
        map2.put("name", "二");
        list.add(map2);
        list.add(map2);

        Map<Object, Object> map3 = new HashMap<>();
        map3.put("code", "3");
        map3.put("name", "三");
        list.add(map3);

        List<Object> code = list.stream().map(e -> e.get("code")).distinct().collect(Collectors.toList());
        System.out.println("code.toString() = " + code.toString());


    }



    public static void listGroup() {
        List<Employee> empoyees = getEmpoyees();
        System.out.println("empoyees = " + empoyees);
        Map<String, Map<String, List<Employee>>> collect = empoyees.stream().collect(Collectors.groupingBy(Employee::getCity, Collectors.groupingBy(Employee::getName)));
        collect.forEach((key, val) ->{
            System.out.println("key = " + key +"    " +val);
        });

    }

    private static List<Employee> getEmpoyees() {
        List<Employee> list = new ArrayList<>();
        Random random = new Random();
        String[] citys = {"北京", "上海", "广州", "杭州"};
        String[] firstname = {"张", "李", "王", "刘"};
        Integer[] sales = {100, 50, 30};
        for (int i = 0; i < 4; i++) {
            for (int j =0; j<3; j++){
                if( i==2 && j==2){
                    continue;
                }
                Employee employee = new Employee(firstname[i], citys[i], sales[j]);
                list.add(employee);
            }


        }
        return list;
    }

    static class Employee {

        private String name;
        private String city;
        private Integer sales;

        public Employee() {
        }

        @Override
        public String toString() {
            return "Employee{" +
                    "name='" + name + '\'' +
                    ", city='" + city + '\'' +
                    ", sales=" + sales +
                    '}';
        }

        public Employee(String name, String city, Integer sales) {
            this.name = name;
            this.city = city;
            this.sales = sales;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public Integer getSales() {
            return sales;
        }

        public void setSales(Integer sales) {
            this.sales = sales;
        }
    }


}
